package com.net.xpay.core.manager.impl.safePay;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractVersionedManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.safePay.SafePayBookAccountMapper;
import com.net.xpay.core.domain.safePay.SafePayBookAccount;
import com.net.xpay.core.dto.SafePayBookAccountCountDto;
import com.net.xpay.core.manager.safePay.SafePayBookAccountManager;
import com.net.xpay.core.model.SafePayBookAccountSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.util.List;


@Component
public class SafePayBookAccountManagerImpl extends AbstractVersionedManager<SafePayBookAccount> implements SafePayBookAccountManager{

    @Autowired
    private SafePayBookAccountMapper mapper;

    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(SafePayBookAccount obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        return super.save(obj);
    }


    @Override
    protected BasicMapper<Long, SafePayBookAccount> getMapper() {
        return mapper;
    }

    @Override
    public SafePayBookAccount findByAlipayNickName(Long userId, String alipayNickName) {
        Preconditions.checkArgument(userId != null && !Strings.isNullOrEmpty(alipayNickName));
        SafePayBookAccount condition = new SafePayBookAccount();
        condition.setUserId(userId);
        condition.setAlipayNickName(alipayNickName);
        return getByCondition(condition);
    }

    @Override
    public SafePayBookAccount findByExternalAgreementNo(String externalAgreementNo) {
        SafePayBookAccount condition = new SafePayBookAccount();
        condition.setExternalAgreementNo(externalAgreementNo);
        return getByCondition(condition);
    }

    @Override
    public SafePayBookAccount findById(Long userId, Long id) {
        Preconditions.checkArgument(userId != null && id != null);
        SafePayBookAccount condition = new SafePayBookAccount();
        condition.setUserId(userId);
        condition.setId(id);
        return getByCondition(condition);
    }

    @Override
    public List<SafePayBookAccount> search(SafePayBookAccountSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(SafePayBookAccountSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public List<SafePayBookAccount> list(SafePayBookAccountSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }

    @Override
    public SafePayBookAccount getByBookId(String bookId) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(bookId));
        SafePayBookAccount condition = new SafePayBookAccount();
        condition.setBookId(bookId);
        return getByCondition(condition);
    }

    @Override
    public SafePayBookAccount getByUserId(Long userId) {
        Preconditions.checkArgument(userId != null);
        SafePayBookAccount condition = new SafePayBookAccount();
        condition.setUserId(userId);
        return getByCondition(condition);
    }

    @Override
    public SafePayBookAccountCountDto searchCountDto(SafePayBookAccountSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCountDto(searchModel);
    }
}
